﻿using System;
using Slaks.Graphs.Analysis;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Slaks.Filters;
using Slaks.GraphExtensions;
using Slaks.GraphExtensions.Algorithms;
using Slaks.GraphExtensions.GraphBuilder;
using Slaks.Graphs;
using Slaks.Graphs.Algorithms;
using Slaks.Graphs.Algorithms.ExperimentalDistances;
using Slaks.Graphs.Category;
using Slaks.Graphs.GraphBuilder;
//using Slaks.Graphs.GraphBuilder.Extractor;
using Slaks.Graphs.GraphBuilder.Schenker;
using Slaks.Graphs.GraphBuilder.Slaks;
using Slaks.Graphs.GraphBuilder.Slaks.ExperimentalRepresentations;
using Slaks.Graphs.ShenkerGraph.Algorithms;
using Slaks.SuperGraphExtensions.Containers;
using Slaks.Web.Parser;
using Slaks.DocumentSerialization;
using System.Collections;
using System.IO;
using Slaks.DocumentSerialization.Document;
//using Slaks.DocumentSerialization.Document.Wordnet;
using Slaks.DocumentSerialization.ParsedFiles;
using Slaks.DocumentSerialization.TermIdInfo;
using Slaks.Readers;
using TextParsing;
using Slaks.Graphs.Algorithms.DistanceAlgorithm;

namespace EndToEnd
{
    public class Runner
    {
        const int PARTITION_COUNT = 30;
        private string _outputPath;
        private string _categoryfileTxt;
        private string _sourceFilesPath;
        private Encoding _currentEncoding;
        private bool _isUseStopwordCollectionModule;
        
        private int _numberOfVerticesToLeave;
        private int _clusterNumber;
        private CategoriesFile _categoriesFile;
        private TermIdInfoMap _termIdMap;
        private IList _parsedFiles;
        private bool _skipParsing;
        private int _kNeighbours;
        private string _outputFilename;
        const string distanceAlgorithmValue1 = "MCS";
        const string distanceAlgorithmValue2 = "WGU";
        const string distanceAlgorithmValue3 = "SlaksMCS";
        const string distanceAlgorithmValue4 = "SlaksWGU";
        const string distanceAlgorithmValue5 = "CommonFeaturesModel";
        const string distanceAlgorithmValue6 = "MCSsubgraph";
        const string distanceAlgorithmValue7 = "WGUext";
        const string distanceAlgorithmValue8 = "MCSbalanced";
        const string distanceAlgorithmValue9 = "MCSmixed";
        const string distanceAlgorithmValue10 = "MCSseparate";
        const string distanceAlgorithmValue11 = "MCSlimited";
        const string distanceAlgorithmValue12 = "MCSadapted";
        const string distanceAlgorithmValue13 = "MCSedge-weight";
        const string distanceAlgorithmValue14 = "WGUbalanced";
        const string distanceAlgorithmValue15 = "WGUseparate";
        const string distanceAlgorithmValue16 = "WGUsubgraph";

        //public static void DoReuters()
        //{
        //    string[] files = Directory.GetFiles(@"C:\ParsingTest\reuters21578_HTML","*.html",SearchOption.AllDirectories);
        //    //foreach (string folder in folders)
        //    //{
        //    //    string targetFolder = Directory.CreateDirectory(Path.Combine(folder, "_parsed")).FullName;
        //        IList parsedFiles;
        //    TermIdInfoMap map;
        //    ParseCollection(files, out parsedFiles,out map);
        //    //}
        //}
        public Runner()
        {
            AddSuperGraphRepresentation();
            AddGraphRepresentation();

        }
        internal void Run2()
        {
            InitializeMembers();

            _categoriesFile = new CategoriesFile();
            _categoriesFile.FillCateogriesFromFile(_categoryfileTxt);
            IList filesToParse = new List<string>(Directory.GetFiles(_sourceFilesPath));
            
            int rslt=0;
            _skipParsing = false;
            if (!_skipParsing)
            {
                rslt = ParseCollection(filesToParse, out _parsedFiles, out _termIdMap);
                if (rslt != 0)
                    throw new Exception();
            }
            else
            {
                ReadMetaDataFromFile();
            }

            List<int>[] partitions = PartitionGraphs(_parsedFiles.Count);
            int numberOfIterations = partitions.Length;
            IDictionary<BuilderType,KeyValuePair<GraphList,GraphDistanceBase>> experiments = new Dictionary<BuilderType, KeyValuePair<GraphList, GraphDistanceBase>>();
            BuilderType representationTypeValue;
            GraphList graphList; 
            GraphDistanceBase distanceMeasureDelegate;
            
            graphList = null;
            representationTypeValue = BuilderType.ExtractorBuilderNodesOnly;
            rslt = LoadGraphs(ref graphList, representationTypeValue);
            if (rslt != 0)
                throw new Exception();
            SetDistanceMesure(distanceAlgorithmValue6, out distanceMeasureDelegate, 0, representationTypeValue);
            experiments.Add(representationTypeValue,new KeyValuePair<GraphList, GraphDistanceBase>(graphList,distanceMeasureDelegate));
            graphList = null;
            representationTypeValue = BuilderType.StandardRepresentationSchenker;
            rslt = LoadGraphs(ref graphList, representationTypeValue);
            if (rslt != 0)
                throw new Exception();
            SetDistanceMesure(distanceAlgorithmValue6, out distanceMeasureDelegate, 0, representationTypeValue);
            experiments.Add(representationTypeValue, new KeyValuePair<GraphList, GraphDistanceBase>(graphList, distanceMeasureDelegate));
            for (int i = 0; i < numberOfIterations; i++)
            {
                RunIteration(i, partitions,experiments);
            }
            
            //ShenkerGraphClustering shenker = null;
            //for (int j = 1; j <= 10; j++)
            //{
            //    shenker = new ShenkerGraphClustering(graphList, _clusterNumber, distanceMeasureDelegate);
            //    shenker.Apply();
            //}
            //GraphList[] clusters = shenker.GetGraphClusters();
            //double randIndex = Slaks.Graphs.Analysis.ClusteringAnalysis.GetRandIndex(shenker.GetGraphClusters(), null);
            //double dunnIndex = Slaks.Graphs.Analysis.ClusteringAnalysis.GetDunnIndex(shenker.GetGraphClusters(), distanceMeasureDelegate);
            //double mutualInfo = Slaks.Graphs.Analysis.ClusteringAnalysis.GetMutualInformation(shenker.GetGraphClusters(), null, CategoryReader.GetCategoryList(), shenker.CountGraphs());

        }


        class ExperimentRunner 
        {
 
        }



        internal void Run3()
        {
            IList<Experiment> experiments = new List<Experiment>();
            Experiment experiment;

            #region Define Experiments

            //experiment = new Experiment
            //                 {
            //                     Task = Experiment.TaskType.Categorization,
            //                     RepresentationType = BuilderType.StandardRepresentationSchenker,
            //                     DistanceAlgorithm = distanceAlgorithmValue6,
            //                     EdgesWeight = 0,
            //                     GraphSize = 30,
            //                     Iterations = 30,
            //                     ResultsFile = @"C:\Slava_Parser\Files\results.csv",
            //                     SkipParsing = false,
            //                     RemoveStopWords = true,
            //                     AdditionalParam = 5
            //                 };
            //experiments.Add(experiment);
            //experiment = new Experiment
            //                 {
            //                     Task = Experiment.TaskType.Categorization,
            //                     RepresentationType = BuilderType.ExtractorBuilderNodesOnly,
            //                     DistanceAlgorithm = distanceAlgorithmValue6,
            //                     EdgesWeight = 0,
            //                     GraphSize = 30,
            //                     Iterations = 30,
            //                     ResultsFile = @"C:\Slava_Parser\Files\results.csv",
            //                     SkipParsing = true,
            //                     RemoveStopWords = true,
            //                     AdditionalParam = 5
            //                 };
            //experiments.Add(experiment);
            //experiment = new Experiment
            //                 {
            //                     Task = Experiment.TaskType.Categorization,
            //                     RepresentationType = BuilderType.StandardRepresentationSchenker,
            //                     DistanceAlgorithm = distanceAlgorithmValue6,
            //                     EdgesWeight = 0,
            //                     GraphSize = 30,
            //                     Iterations = 30,
            //                     ResultsFile = @"C:\Slava_Parser\Files\results.csv",
            //                     SkipParsing = true,
            //                     RemoveStopWords = true,
            //                     AdditionalParam = 4
            //                 };
            //experiments.Add(experiment);
            //experiment = new Experiment
            //                 {
            //                     Task = Experiment.TaskType.Categorization,
            //                     RepresentationType = BuilderType.ExtractorBuilderNodesOnly,
            //                     DistanceAlgorithm = distanceAlgorithmValue6,
            //                     EdgesWeight = 0,
            //                     GraphSize = 30,
            //                     Iterations = 30,
            //                     ResultsFile = @"C:\Slava_Parser\Files\results.csv",
            //                     SkipParsing = true,
            //                     RemoveStopWords = true,
            //                     AdditionalParam = 4
            //                 };
            //experiment = new Experiment
            //                 {
            //                     Task = Experiment.TaskType.Categorization,
            //                     RepresentationType = BuilderType.StandardRepresentationSchenker,
            //                     DistanceAlgorithm = distanceAlgorithmValue6,
            //                     EdgesWeight = 0,
            //                     GraphSize = 30,
            //                     Iterations = 30,
            //                     ResultsFile = @"C:\Slava_Parser\Files\results.csv",
            //                     SkipParsing = true,
            //                     RemoveStopWords = true,
            //                     AdditionalParam = 3
            //                 };
            //experiments.Add(experiment);
            //experiment = new Experiment
            //                 {
            //                     Task = Experiment.TaskType.Categorization,
            //                     RepresentationType = BuilderType.ExtractorBuilderNodesOnly,
            //                     DistanceAlgorithm = distanceAlgorithmValue6,
            //                     EdgesWeight = 0,
            //                     GraphSize = 30,
            //                     Iterations = 30,
            //                     ResultsFile = @"C:\Slava_Parser\Files\results.csv",
            //                     SkipParsing = true,
            //                     RemoveStopWords = true,
            //                     AdditionalParam = 3
            //                 };
            //experiment = new Experiment
            //                 {
            //                     Task = Experiment.TaskType.Categorization,
            //                     RepresentationType = BuilderType.StandardRepresentationSchenker,
            //                     DistanceAlgorithm = distanceAlgorithmValue6,
            //                     EdgesWeight = 0,
            //                     GraphSize = 30,
            //                     Iterations = 30,
            //                     ResultsFile = @"C:\Slava_Parser\Files\results.csv",
            //                     SkipParsing = true,
            //                     RemoveStopWords = true,
            //                     AdditionalParam = 2
            //                 };
            //experiments.Add(experiment);
            //experiment = new Experiment
            //                 {
            //                     Task = Experiment.TaskType.Categorization,
            //                     RepresentationType = BuilderType.ExtractorBuilderNodesOnly,
            //                     DistanceAlgorithm = distanceAlgorithmValue6,
            //                     EdgesWeight = 0,
            //                     GraphSize = 30,
            //                     Iterations = 30,
            //                     ResultsFile = @"C:\Slava_Parser\Files\results.csv",
            //                     SkipParsing = true,
            //                     RemoveStopWords = true,
            //                     AdditionalParam = 2
            //                 };
            //experiment = new Experiment
            //                 {
            //                     Task = Experiment.TaskType.Categorization,
            //                     RepresentationType = BuilderType.StandardRepresentationSchenker,
            //                     DistanceAlgorithm = distanceAlgorithmValue6,
            //                     EdgesWeight = 0,
            //                     GraphSize = 30,
            //                     Iterations = 30,
            //                     ResultsFile = @"C:\Slava_Parser\Files\results.csv",
            //                     SkipParsing = true,
            //                     RemoveStopWords = true,
            //                     AdditionalParam = 6
            //                 };
            //experiments.Add(experiment);
            //experiment = new Experiment
            //                 {
            //                     Task = Experiment.TaskType.Categorization,
            //                     RepresentationType = BuilderType.ExtractorBuilderNodesOnly,
            //                     DistanceAlgorithm = distanceAlgorithmValue6,
            //                     EdgesWeight = 0,
            //                     GraphSize = 30,
            //                     Iterations = 30,
            //                     ResultsFile = @"C:\Slava_Parser\Files\results.csv",
            //                     SkipParsing = true,
            //                     RemoveStopWords = true,
            //                     AdditionalParam = 6
            //                 };
            //experiment = new Experiment
            //                 {
            //                     Task = Experiment.TaskType.Categorization,
            //                     RepresentationType = BuilderType.StandardRepresentationSchenker,
            //                     DistanceAlgorithm = distanceAlgorithmValue6,
            //                     EdgesWeight = 0,
            //                     GraphSize = 30,
            //                     Iterations = 30,
            //                     ResultsFile = @"C:\Slava_Parser\Files\results.csv",
            //                     SkipParsing = true,
            //                     RemoveStopWords = true,
            //                     AdditionalParam = 7
            //                 };
            //experiments.Add(experiment);
            //experiment = new Experiment
            //                 {
            //                     Task = Experiment.TaskType.Categorization,
            //                     RepresentationType = BuilderType.ExtractorBuilderNodesOnly,
            //                     DistanceAlgorithm = distanceAlgorithmValue6,
            //                     EdgesWeight = 0,
            //                     GraphSize = 30,
            //                     Iterations = 30,
            //                     ResultsFile = @"C:\Slava_Parser\Files\results.csv",
            //                     SkipParsing = true,
            //                     RemoveStopWords = true,
            //                     AdditionalParam = 7
            //                 };
            //experiments.Add(experiment);
            //experiment = new Experiment
            //                 {
            //                     Task = Experiment.TaskType.Clustering,
            //                     RepresentationType = BuilderType.StandardRepresentationSchenker,
            //                     DistanceAlgorithm = distanceAlgorithmValue6,
            //                     EdgesWeight = 0,
            //                     GraphSize = 30,
            //                     Iterations = 10,
            //                     ResultsFile = @"C:\Slava_Parser\Files\results.csv",
            //                     SkipParsing = true,
            //                     RemoveStopWords = true
            //                 };
            //experiments.Add(experiment);
            experiment = new Experiment
            {
                Task = Experiment.TaskType.Categorization,
                RepresentationType = BuilderType.StandardRepresentationSchenker,
                DistanceAlgorithm = distanceAlgorithmValue1,
                EdgesWeight = 0,
                GraphSize = 30,
                Iterations = 30,
                AdditionalParam = 3,
                ResultsFile = @"C:\Slava_Parser\Files\results7.csv",
                SkipParsing = true,
                RemoveStopWords = true
            };

            experiments.Add(experiment);
            experiment = new Experiment
            {
                Task = Experiment.TaskType.Categorization,
                RepresentationType = BuilderType.StandardRepresentationSchenker,
                DistanceAlgorithm = distanceAlgorithmValue1,
                EdgesWeight = 0,
                GraphSize = 30  ,
                Iterations = 30,
                AdditionalParam =2,
                ResultsFile = @"C:\Slava_Parser\Files\results7.csv",
                SkipParsing = true,
                RemoveStopWords = true
            };

            experiments.Add(experiment);
            experiment = new Experiment
            {
                Task = Experiment.TaskType.Categorization,
                RepresentationType = BuilderType.StandardRepresentationSchenker,
                DistanceAlgorithm = distanceAlgorithmValue1,
                EdgesWeight = 0,
                GraphSize = 30,
                Iterations = 30,
                AdditionalParam = 5,
                ResultsFile = @"C:\Slava_Parser\Files\results7.csv",
                SkipParsing = true,
                RemoveStopWords = true
            };

            experiments.Add(experiment);
            experiment = new Experiment
            {
                Task = Experiment.TaskType.Categorization,
                RepresentationType = BuilderType.ExtractorBuilderNodesOnly,
                DistanceAlgorithm = distanceAlgorithmValue1,
                EdgesWeight = 0,
                GraphSize = 30,
                Iterations = 30,
                AdditionalParam = 5,
                ResultsFile = @"C:\Slava_Parser\Files\results7.csv",
                SkipParsing = true,
                RemoveStopWords = true
            };

            experiments.Add(experiment);
            //experiment = new Experiment
            //{
            //    Task = Experiment.TaskType.Categorization,
            //    RepresentationType = BuilderType.ExtractorBuilderNodesOnly,
            //    DistanceAlgorithm = distanceAlgorithmValue1,
            //    EdgesWeight = 0,
            //    GraphSize = 30,
            //    Iterations = 30,
            //    AdditionalParam = 2,
            //    ResultsFile = @"C:\Slava_Parser\Files\results4.csv",
            //    SkipParsing = true,
            //    RemoveStopWords = true
            //};
            //experiments.Add(experiment); 
            #endregion

            List<int>[][] partitions = new List<int>[4][];
            foreach (Experiment exp in experiments)
            {
                exp.SetDocumentsCollection("J-Series", @"C:\Slava_Parser\Files\RawSeries\J series", @"C:\Slava_Parser\Files\RawSeries\J series\_parsed", @"C:\Slava_Parser\Files\JCategory.txt", Encoding.UTF8);
                ////if (partitions[2] != null)
                ////{
                ////    exp.Partitions = partitions[0];
                ////}
                exp.RunExperiment();
                //if (partitions[2] == null)
                //{
                //    partitions[2] = exp.Partitions;
                //}
                exp.SetDocumentsCollection("F-Series", @"C:\Slava_Parser\files\RawSeries\F series", @"C:\Slava_Parser\Files\RawSeries\F series\_parsed", @"C:\Slava_Parser\Files\FCategory.txt", Encoding.UTF8);
                ////if (partitions[3] != null)
                ////{
                ////    exp.Partitions = partitions[0];
                ////}
                exp.RunExperiment();
                //if (partitions[3] == null)
                //{
                //    partitions[3] = exp.Partitions;
                //}
                
                //exp.SetDocumentsCollection("ReutersSubset", @"C:\ParsingTest\ReutersSubset", @"C:\ParsingTest\ReutersSubset\_parsed", @"C:\ParsingTest\CategorySubset.txt", Encoding.UTF8);
                ////if (partitions[0] != null)
                ////{
                ////    exp.Partitions = partitions[0];
                ////}
                //exp.RunExperiment();
                //exp.SkipParsing = true;
                //if (partitions[0] == null)
                //{
                //    partitions[0] = exp.Partitions;
                //}

                //exp.SetDocumentsCollection("K-Series", @"C:\Slava_Parser\Files\RawSeries\K series", @"C:\Slava_Parser\Files\RawSeries\K series\_parsed", @"C:\Slava_Parser\Files\KCategory.txt", Encoding.UTF8);
                //if (partitions[1] !=null)
                //{
                //    exp.Partitions = partitions[0];
                //}
                exp.RunExperiment();
                //if (partitions[1] == null)
                //{
                //    partitions[1] = exp.Partitions;
                //}
                //exp.SkipParsing = true;
                
                
            }
            
        }
        private void RunIteration(int iteration, List<int>[] partitions, IDictionary<BuilderType,KeyValuePair<GraphList,GraphDistanceBase>> graphingAlgorithms)
        {
            foreach (KeyValuePair<BuilderType, KeyValuePair<GraphList, GraphDistanceBase>> algorithm in graphingAlgorithms)
            {
                BuilderType _representationTypeValue = algorithm.Key;
                GraphList graphList = algorithm.Value.Key;
                GraphDistanceBase graphDistanceMeasure = algorithm.Value.Value;
                RunGraphingMethod(graphList, _representationTypeValue, partitions, iteration,graphDistanceMeasure);
            }
        }

        private void RunGraphingMethod(GraphList graphList, BuilderType representationTypeValue, List<int>[] partitions, int iteration, GraphDistanceBase distanceMeasureDelegate)
        {
            GraphList trainingSet;
            GraphList testSet;
            GenerateTrainingAndTestSets(partitions, graphList,out trainingSet, out testSet, iteration);
            var classifying = new ShenkerGraphClassifying(trainingSet, _kNeighbours, distanceMeasureDelegate);
            var resultsSet = classifying.Apply(testSet, _categoriesFile.DocumentToCategoryMap);
            var accuracy = IRAnalysis.GetAccuracy(resultsSet, _categoriesFile);

            SaveResults("Classification",iteration, representationTypeValue.ToString(), _sourceFilesPath, accuracy);
        }

        private void SaveResults(string task,int iteration, string graphingAlgorithm, string dataSeries, params double[] measurements)
        {
            var outputFile = new StreamWriter(_outputFilename,true);
            outputFile.Write(task);
            outputFile.Write(',');
            outputFile.Write(iteration);
            outputFile.Write(',');
            outputFile.Write(graphingAlgorithm);
            outputFile.Write(',');
            outputFile.Write(dataSeries);
            outputFile.Write(',');
            for (int i = 0; i < measurements.Length; i++)
            {
                outputFile.Write(measurements[i]);
                outputFile.Write(',');

            }
            outputFile.WriteLine();
            outputFile.Close();
        }

        private void GenerateTrainingAndTestSets(List<int>[] partitions, GraphList graphs,out GraphList trainingSet, out GraphList testSet, int iteration)
        {
            trainingSet = new GraphList();
            testSet = new GraphList();
            for (int i = 0; i < partitions.Length; i++)
            {
                if (i==iteration)
                {
                    foreach (int idx in partitions[i])
                    {
                        testSet.Add(graphs[idx]);
                    }
                }
                else
                {
                    foreach (int idx in partitions[i])
                    {
                        trainingSet.Add(graphs[idx]);
                    }

                }
            }
        }

        private List<int>[] PartitionGraphs(int totalAmount)
        {
            Random rnd = new Random((int)DateTime.Now.Ticks);
            
            var result = new List<int>[PARTITION_COUNT];
            List<int> indices = new List<int>(totalAmount);
            for (int i = 0; i < totalAmount; i++)
            {
                indices.Add(i);
            }
            int partitionSize =(int) Math.Floor(totalAmount*1.0/PARTITION_COUNT);
            
                for (int i = 0; i < PARTITION_COUNT; i++)
                {
                    result[i] = new List<int>();
                    for (int j = 0; j < partitionSize; j++)
                    {
                        int idx = rnd.Next(0, indices.Count);
                        result[i].Add(indices[idx]);
                        indices.RemoveAt(idx);
                    }
                }
                if (indices.Count > 0)
                {
                    for (int i = 0; i < PARTITION_COUNT && indices.Count > 0; i++)
                    {
                        int idx = rnd.Next(0, indices.Count);
                        result[i].Add(indices[idx]);
                        indices.RemoveAt(idx);
                    }
                }
            return result;
        }

        private void ReadMetaDataFromFile()
        {
            const string parsedFileListXml = @"parsed_file_list.xml";
            string parsedFilelistName = Path.Combine(_outputPath, parsedFileListXml);
            if (File.Exists(parsedFilelistName))
            {
                _parsedFiles = ParsedFiles.ReadXml(parsedFilelistName);
            }
            const string termIdMap = @"term_id_info.xml";
            string mapFileName = Path.Combine(_outputPath, termIdMap);
            if (File.Exists(mapFileName))
            {
                _termIdMap = TermIdInfo.ReadXml(mapFileName);
            }
        }

        private void InitializeMembers()
        {
            _outputPath = @"C:\Slava_Parser\Files\RawSeries\F series\_parsed";
            _outputFilename=@"C:\ParsingTest\Temp\results.csv";
            _categoryfileTxt = @"C:\Slava_Parser\Files\FCategory.txt";
            _sourceFilesPath = @"C:\Slava_Parser\Files\RawSeries\F series";
            _currentEncoding = Encoding.UTF8;
            _isUseStopwordCollectionModule = false;
            _numberOfVerticesToLeave = 30;
            _clusterNumber = 2;
            _kNeighbours = 5;
        }

        int ParseCollection(IList filesToParse, out IList parsedFilesList, out TermIdInfoMap map)
        {
            int retVal = -1;
            try
            {
                if (!Directory.Exists(_outputPath))
                    Directory.CreateDirectory(_outputPath);
                var stopWordsFile = @"C:\Slava_Parser\Dlls\english_stopwords_schenker.txt";
                var bufferReaderCore = new TermBufferReader(new EnglishTermFilter());
                var structureConstruction = new StructureConstruction(bufferReaderCore, _outputPath, _currentEncoding,null ,null);
                structureConstruction.ProceedParsing(filesToParse);
                map = structureConstruction.TermIdMap;
                parsedFilesList = ParsedFiles.ReadXml(structureConstruction.ParsedFileList);
                retVal = 0;
            }
            catch (Exception)
            {
                retVal = - 1;
                parsedFilesList = null;
                map = null;
            }
            return retVal;
        }
        private static void AddSuperGraphRepresentation()
        {
            //slaks representation
            SuperGraphBuilderFactory.AddGraphBuilder(BuilderType.StandardRepresentationSlaks, new StandardRepresentationExtendedSlaks());
        }
        static int LoadSuperGraphs(IEnumerable parsedFiles, BuilderType representationTypeValue, int numberOfVerticesToLeave, IList graphList)
        {
            int retVal = -1;

            if (graphList == null)
            {
                graphList = new ArrayList();
            }
            
            try
            {
                foreach (string s in parsedFiles)
                {
                    
                    Document document = Xml2Document.ReadXml(s);
                    
                    IRepresentationExtended representation = SuperGraphBuilderFactory.GetGraphBuilder(representationTypeValue);
                    try
                    {
                        SuperGraph newgraph;
                        if (representation!=null)
                        {
                            newgraph = representation.Build(document, numberOfVerticesToLeave);
                        }

                        else throw new Exception("Invalid representation : " + representationTypeValue);

                        graphList.Add(newgraph);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        
                        //                    numberOfMissedGraphs++;
                    }
                }
                retVal = 0;
            }
            catch
            {
                retVal = -1;
            }
            return retVal;
        }
        private static void AddGraphRepresentation()
        {
            GraphBuilderFactory.Instance.AddGraphBuilder(BuilderType.StandardRepresentationSchenker, new StandardRepresentationSchenker());
            GraphBuilderFactory.Instance.AddGraphBuilder(BuilderType.SimpleRepresentationSchenker, new SimpleRepresentationSchenker());
            GraphBuilderFactory.Instance.AddGraphBuilder(BuilderType.NDistanceRepresentationSchenker, new NDistanceRepresentationSchenker());
            GraphBuilderFactory.Instance.AddGraphBuilder(BuilderType.NSimpleDistanceRepresentationSchenker, new NSimpleDistanceRepresentationSchenker());
            GraphBuilderFactory.Instance.AddGraphBuilder(BuilderType.RawFrequencyRepresentationSchenker, new RawFrequencyRepresentationSchenker());
            GraphBuilderFactory.Instance.AddGraphBuilder(BuilderType.NormalizedFrequencyRepresentationSchenker, new NormalizedFrequencyRepresentationSchenker());

            //slaks representation
            GraphBuilderFactory.Instance.AddGraphBuilder(BuilderType.StandardRepresentationSlaks, new StandardRepresentationSlaks());
            GraphBuilderFactory.Instance.AddGraphBuilder(BuilderType.SimpleRepresentationSlaks, new SimpleRepresentationSlaks());
            GraphBuilderFactory.Instance.AddGraphBuilder(BuilderType.NDistanceRepresentationSlaks, new NDistanceRepresentationSlaks());
            GraphBuilderFactory.Instance.AddGraphBuilder(BuilderType.NSimpleDistanceRepresentationSlaks, new NSimpleDistanceRepresentationSlaks());
            GraphBuilderFactory.Instance.AddGraphBuilder(BuilderType.RawFrequencyRepresentationSlaks, new RawFrequencyRepresentationSlaks());
            GraphBuilderFactory.Instance.AddGraphBuilder(BuilderType.NormalizedFrequencyRepresentationSlaks, new NormalizedFrequencyRepresentationSlaks());

            GraphBuilderFactory.Instance.AddGraphBuilder(BuilderType.StandardSimpleRepresentationSlaks, new StandardSimpleRepresentationSlaks());

            GraphBuilderFactory.Instance.AddGraphBuilder(BuilderType.StandardBiderectionalRepresentationSlaks, new StandardBiderectionalRepresentationSlaks());
            GraphBuilderFactory.Instance.AddGraphBuilder(BuilderType.NDistanceBiderectionalRepresentationSlaks, new NDistanceBiderectionalRepresentationSlaks());

//            GraphBuilderFactory.Instance.AddGraphBuilder(BuilderType.ExtractorBuilderNodesOnly, new ExtractorBuilderNodesOnly());
        }
        int LoadGraphs(ref GraphList graphList, BuilderType representationTypeValue)
        {
            int retVal = -1;

            if (graphList == null)
            {
                graphList = new GraphList();
            }

            try
            {
                foreach (string s in _parsedFiles)
                {

                    Document document = Xml2Document.ReadXml(s);

                    IRepresentation representation = GraphBuilderFactory.Instance.GetInstance(representationTypeValue.ToString("F"));
                    try
                    {
                        Graph newgraph;
                        if (representation != null)
                        {
                            newgraph = representation.Build(document, _numberOfVerticesToLeave,_termIdMap);
                            newgraph.Category = _categoriesFile.DocumentToCategoryMap[Path.GetFileName(newgraph.Name)];
                        }

                        else throw new Exception("Invalid representation : " + representationTypeValue);

                        graphList.Add(newgraph);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);

                        //                    numberOfMissedGraphs++;
                    }
                }
                retVal = 0;
            }
            catch
            {
                retVal = -1;
            }
            return retVal;
        }

        static void SetDistanceMesure(string distAlgorithmName, out SuperGraphDistanceMeasure distanceMeasureDelegate, BuilderType representationTypeValue, double edgeWeightValue)
        {
            if (distAlgorithmName != null)
            {
                if (distAlgorithmName == distanceAlgorithmValue1)
                {
                    if (representationTypeValue == BuilderType.StandardRepresentationSchenker || representationTypeValue == BuilderType.StandardRepresentationSlaks ||
                        representationTypeValue == BuilderType.SimpleRepresentationSchenker || representationTypeValue == BuilderType.SimpleRepresentationSlaks ||
                        representationTypeValue == BuilderType.NDistanceRepresentationSchenker || representationTypeValue == BuilderType.NDistanceRepresentationSlaks ||
                        representationTypeValue == BuilderType.NSimpleDistanceRepresentationSchenker || representationTypeValue == BuilderType.NSimpleDistanceRepresentationSlaks)
                        distanceMeasureDelegate = new SuperGraphDistanceMeasure(SuperGraphDistances.DistMCS4VertexEdgeCount);
                    else
                        distanceMeasureDelegate = new SuperGraphDistanceMeasure(SuperGraphDistances.DistMCS4VertexEdgeFrequency);

                    Console.WriteLine("MCS distance algorithm set with representation : " + representationTypeValue.ToString());
                }
                else if (distAlgorithmName == distanceAlgorithmValue2)
                {
                    if (representationTypeValue == BuilderType.StandardRepresentationSchenker || representationTypeValue == BuilderType.StandardRepresentationSlaks ||
                        representationTypeValue == BuilderType.SimpleRepresentationSchenker || representationTypeValue == BuilderType.SimpleRepresentationSlaks ||
                        representationTypeValue == BuilderType.NDistanceRepresentationSchenker || representationTypeValue == BuilderType.NDistanceRepresentationSlaks ||
                        representationTypeValue == BuilderType.NSimpleDistanceRepresentationSchenker || representationTypeValue == BuilderType.NSimpleDistanceRepresentationSlaks)
                        distanceMeasureDelegate = new SuperGraphDistanceMeasure(SuperGraphDistances.DistWGU4VertexEdgeCount);
                    else
                        distanceMeasureDelegate = new SuperGraphDistanceMeasure(SuperGraphDistances.DistWGU4VertexEdgeFrequency);

                    Console.WriteLine("WGU distance algorithm set with representation : " + representationTypeValue.ToString());
                }
                /*
            else if (distAlgorithmName == distanceAlgorithmValue3)
            {
                if (representationTypeValue == BuilderType.StandardRepresentationSchenker || representationTypeValue == BuilderType.StandardRepresentationSlaks ||
                    representationTypeValue == BuilderType.SimpleRepresentationSchenker || representationTypeValue == BuilderType.SimpleRepresentationSlaks ||
                    representationTypeValue == BuilderType.NDistanceRepresentationSchenker || representationTypeValue == BuilderType.NDistanceRepresentationSlaks ||
                    representationTypeValue == BuilderType.NSimpleDistanceRepresentationSchenker || representationTypeValue == BuilderType.NSimpleDistanceRepresentationSlaks)
                    distanceMeasureDelegate = new SuperGraphDistanceMeasure(SlaksGraphDistances.DistMCS4VertexEdgeCount);
                else
                    throw new Exception(representationTypeValue + " is not compatible with SlaksMCS distance measure");

                writer.WriteLine("SlaksMCS distance algorithm set");
            }
            else if (distAlgorithmName == distanceAlgorithmValue4)
            {
                if (representationTypeValue == BuilderType.StandardRepresentationSchenker || representationTypeValue == BuilderType.StandardRepresentationSlaks ||
                    representationTypeValue == BuilderType.SimpleRepresentationSchenker || representationTypeValue == BuilderType.SimpleRepresentationSlaks ||
                    representationTypeValue == BuilderType.NDistanceRepresentationSchenker || representationTypeValue == BuilderType.NDistanceRepresentationSlaks ||
                    representationTypeValue == BuilderType.NSimpleDistanceRepresentationSchenker || representationTypeValue == BuilderType.NSimpleDistanceRepresentationSlaks)
                    distanceMeasureDelegate = new SuperGraphDistanceMeasure(SlaksGraphDistances.DistWGU4VertexEdgeCount);
                else
                    throw new Exception(representationTypeValue + " is not compatible with SlaksWGU distance measure");

                writer.WriteLine("SlaksWGU distance algorithm set");
            }
            */
                else
                {
                    Console.WriteLine("Uknown distance algorithm : " + distAlgorithmName + " -> Must be [MSC,WGU,SlaksMCS,SkaksWGU]");
                    distanceMeasureDelegate = null;
                    return;
                }

            }
            else
            {
                Console.WriteLine("No distance algorithm set");
                throw new Exception("No distance algorithm set");
            }

        }

        void SetDistanceMesure(string distAlgorithmName, out GraphDistanceBase distanceMeasureDelegate,double edgeWeightValue, BuilderType _representationTypeValue)
        {

            if (distAlgorithmName != null)
            {
                if (distAlgorithmName == distanceAlgorithmValue1)
                {
                    if (_representationTypeValue == BuilderType.StandardRepresentationSchenker ||
                        _representationTypeValue == BuilderType.ExtractorBuilderNodesOnly ||
                        _representationTypeValue == BuilderType.StandardRepresentationSlaks ||
                        _representationTypeValue == BuilderType.SimpleRepresentationSchenker ||
                        _representationTypeValue == BuilderType.SimpleRepresentationSlaks ||
                        _representationTypeValue == BuilderType.NDistanceRepresentationSchenker ||
                        _representationTypeValue == BuilderType.NDistanceRepresentationSlaks ||
                        _representationTypeValue == BuilderType.NSimpleDistanceRepresentationSchenker ||
                        _representationTypeValue == BuilderType.NSimpleDistanceRepresentationSlaks)
                        distanceMeasureDelegate = new DistMCS4VertexEdgeCount();
                    else
                        distanceMeasureDelegate = new DistMCS4VertexEdgeFrequency();

                    Console.WriteLine("MCS distance algorithm set with representation : " +
                                      _representationTypeValue.ToString());
                    //writer.WriteLine("MCS distance algorithm set with representation : " +
                    //                 _representationTypeValue.ToString());
                }
                else if (distAlgorithmName == distanceAlgorithmValue2)
                {
                    if (_representationTypeValue == BuilderType.StandardRepresentationSchenker ||
                        _representationTypeValue == BuilderType.ExtractorBuilderNodesOnly ||
                        _representationTypeValue == BuilderType.StandardRepresentationSlaks ||
                        _representationTypeValue == BuilderType.SimpleRepresentationSchenker ||
                        _representationTypeValue == BuilderType.SimpleRepresentationSlaks ||
                        _representationTypeValue == BuilderType.NDistanceRepresentationSchenker ||
                        _representationTypeValue == BuilderType.NDistanceRepresentationSlaks ||
                        _representationTypeValue == BuilderType.NSimpleDistanceRepresentationSchenker ||
                        _representationTypeValue == BuilderType.NSimpleDistanceRepresentationSlaks)
                        distanceMeasureDelegate = new DistWGU4VertexEdgeCount();
                    else
                        distanceMeasureDelegate = new DistWGU4VertexEdgeFrequency();

                    Console.WriteLine("WGU distance algorithm set with representation : " +
                                      _representationTypeValue.ToString());
                    //writer.WriteLine("WGU distance algorithm set with representation : " +
                    //                 _representationTypeValue.ToString());
                }
                else if (distAlgorithmName == distanceAlgorithmValue3)
                {
                    if (_representationTypeValue == BuilderType.StandardRepresentationSchenker ||
                        _representationTypeValue == BuilderType.ExtractorBuilderNodesOnly ||
                        _representationTypeValue == BuilderType.StandardRepresentationSlaks ||
                        _representationTypeValue == BuilderType.SimpleRepresentationSchenker ||
                        _representationTypeValue == BuilderType.SimpleRepresentationSlaks ||
                        _representationTypeValue == BuilderType.NDistanceRepresentationSchenker ||
                        _representationTypeValue == BuilderType.NDistanceRepresentationSlaks ||
                        _representationTypeValue == BuilderType.NSimpleDistanceRepresentationSchenker ||
                        _representationTypeValue == BuilderType.NSimpleDistanceRepresentationSlaks)
                        distanceMeasureDelegate = new DistMCS4VertexEdgeCount();
                    else
                        throw new Exception(_representationTypeValue +
                                            " is not compatible with SlaksMCS distance measure");

                    Console.WriteLine("SlaksMCS distance algorithm set");
                    //writer.WriteLine("SlaksMCS distance algorithm set");
                }
                else if (distAlgorithmName == distanceAlgorithmValue4)
                {
                    if (_representationTypeValue == BuilderType.StandardRepresentationSchenker ||
                        _representationTypeValue == BuilderType.ExtractorBuilderNodesOnly ||
                        _representationTypeValue == BuilderType.StandardRepresentationSlaks ||
                        _representationTypeValue == BuilderType.SimpleRepresentationSchenker ||
                        _representationTypeValue == BuilderType.SimpleRepresentationSlaks ||
                        _representationTypeValue == BuilderType.NDistanceRepresentationSchenker ||
                        _representationTypeValue == BuilderType.NDistanceRepresentationSlaks ||
                        _representationTypeValue == BuilderType.NSimpleDistanceRepresentationSchenker ||
                        _representationTypeValue == BuilderType.NSimpleDistanceRepresentationSlaks)
                        distanceMeasureDelegate = new DistWGU4VertexEdgeCount();
                    else
                        throw new Exception(_representationTypeValue +
                                            " is not compatible with SlaksWGU distance measure");

                    Console.WriteLine("SlaksWGU distance algorithm set");
                    //writer.WriteLine("SlaksWGU distance algorithm set");
                }
                else if (distAlgorithmName == distanceAlgorithmValue5)
                {
                    if (_representationTypeValue == BuilderType.StandardRepresentationSchenker ||
                        _representationTypeValue == BuilderType.ExtractorBuilderNodesOnly ||
                        _representationTypeValue == BuilderType.StandardRepresentationSlaks ||
                        _representationTypeValue == BuilderType.SimpleRepresentationSchenker ||
                        _representationTypeValue == BuilderType.SimpleRepresentationSlaks ||
                        _representationTypeValue == BuilderType.NDistanceRepresentationSchenker ||
                        _representationTypeValue == BuilderType.NDistanceRepresentationSlaks ||
                        _representationTypeValue == BuilderType.NSimpleDistanceRepresentationSchenker ||
                        _representationTypeValue == BuilderType.NSimpleDistanceRepresentationSlaks)
                        distanceMeasureDelegate = new DistCommonFeaturesModel ();
                    else
                        throw new Exception(_representationTypeValue +
                                            " is not compatible with CommonFeaturesModel distance measure");

                    Console.WriteLine("CommonFeaturesModel distance algorithm set");
                    //writer.WriteLine("CommonFeaturesModel distance algorithm set");
                }
                else if (distAlgorithmName == distanceAlgorithmValue6)
                {
                    if (_representationTypeValue == BuilderType.StandardRepresentationSchenker ||
                        _representationTypeValue == BuilderType.ExtractorBuilderNodesOnly ||
                        _representationTypeValue == BuilderType.StandardRepresentationSlaks ||
                        _representationTypeValue == BuilderType.SimpleRepresentationSchenker ||
                        _representationTypeValue == BuilderType.SimpleRepresentationSlaks ||
                        _representationTypeValue == BuilderType.NDistanceRepresentationSchenker ||
                        _representationTypeValue == BuilderType.NDistanceRepresentationSlaks ||
                        _representationTypeValue == BuilderType.NSimpleDistanceRepresentationSchenker ||
                        _representationTypeValue == BuilderType.NSimpleDistanceRepresentationSlaks)
                        distanceMeasureDelegate = new MCSSubgraphVertexEdgeCount();
                    else if (_representationTypeValue == BuilderType.NormalizedFrequencyRepresentationSchenker ||
                             _representationTypeValue == BuilderType.NormalizedFrequencyRepresentationSlaks ||
                             _representationTypeValue == BuilderType.RawFrequencyRepresentationSchenker ||
                             _representationTypeValue == BuilderType.RawFrequencyRepresentationSlaks
                        )
                        distanceMeasureDelegate =
                            new MCSSubgraphVertexEdgeFrequency ();
                    else
                        throw new Exception(_representationTypeValue +
                                            " is not compatible with MCSsubgraph distance measure");

                    Console.WriteLine("MCSsubgraph distance algorithm set");
                    //writer.WriteLine("MCSsubgraph distance algorithm set");
                }
                else if (distAlgorithmName == distanceAlgorithmValue10)
                {
                    if (_representationTypeValue == BuilderType.StandardRepresentationSchenker ||
                        _representationTypeValue == BuilderType.ExtractorBuilderNodesOnly ||
                        _representationTypeValue == BuilderType.StandardRepresentationSlaks ||
                        _representationTypeValue == BuilderType.SimpleRepresentationSchenker ||
                        _representationTypeValue == BuilderType.SimpleRepresentationSlaks ||
                        _representationTypeValue == BuilderType.NDistanceRepresentationSchenker ||
                        _representationTypeValue == BuilderType.NDistanceRepresentationSlaks ||
                        _representationTypeValue == BuilderType.NSimpleDistanceRepresentationSchenker ||
                        _representationTypeValue == BuilderType.NSimpleDistanceRepresentationSlaks)
                        distanceMeasureDelegate = new MCSseparate();
                    else
                        throw new Exception(_representationTypeValue +
                                            " is not compatible with MCSseparate distance measure");

                    Console.WriteLine("MCSseparate distance algorithm set");
                    //writer.WriteLine("MCSseparate distance algorithm set");
                }
                else if (distAlgorithmName == distanceAlgorithmValue8)
                {
                    if (_representationTypeValue == BuilderType.StandardRepresentationSchenker ||
                        _representationTypeValue == BuilderType.ExtractorBuilderNodesOnly ||
                        _representationTypeValue == BuilderType.StandardRepresentationSlaks)
                        distanceMeasureDelegate = new MCSbalancedDistance();
                    else if (_representationTypeValue == BuilderType.StandardSimpleRepresentationSlaks)
                        distanceMeasureDelegate = new MCSbalancedDistance ();
                    else
                        throw new Exception(_representationTypeValue +
                                            " is not compatible with MCSbalanced distance measure");

                    Console.WriteLine("MCSbalanced distance algorithm set");
                    //writer.WriteLine("MCSbalanced distance algorithm set");
                }
                else if (distAlgorithmName == distanceAlgorithmValue9)
                {
                    if (_representationTypeValue == BuilderType.StandardRepresentationSchenker ||
                        _representationTypeValue == BuilderType.ExtractorBuilderNodesOnly ||
                        _representationTypeValue == BuilderType.StandardRepresentationSlaks)
                        distanceMeasureDelegate = new MCSmixed();
                    else
                        throw new Exception(_representationTypeValue +
                                            " is not compatible with MCSmixed distance measure");

                    Console.WriteLine("MCSmixed distance algorithm set");
                    //writer.WriteLine("MCSmixed distance algorithm set");
                }
                else if (distAlgorithmName == distanceAlgorithmValue11)
                {
                    if (_representationTypeValue == BuilderType.StandardRepresentationSchenker ||
                        _representationTypeValue == BuilderType.ExtractorBuilderNodesOnly ||
                        _representationTypeValue == BuilderType.StandardRepresentationSlaks)
                        distanceMeasureDelegate = new MCSLimitedVertexEdgeCount ();
                    else if (_representationTypeValue == BuilderType.RawFrequencyRepresentationSlaks ||
                             _representationTypeValue == BuilderType.NormalizedFrequencyRepresentationSlaks)
                        distanceMeasureDelegate = new MCSLimitedVertexEdgeFrequency();
                    else
                        throw new Exception(_representationTypeValue +
                                            " is not compatible with MCSlimited distance measure");

                    Console.WriteLine("MCSlimited distance algorithm set");
                    //writer.WriteLine("MCSlimited distance algorithm set");
                }
                else if (distAlgorithmName == distanceAlgorithmValue12)
                {
                    if (_representationTypeValue == BuilderType.StandardRepresentationSchenker ||
                        _representationTypeValue == BuilderType.ExtractorBuilderNodesOnly ||
                        _representationTypeValue == BuilderType.StandardRepresentationSlaks)
                        distanceMeasureDelegate = new MCSAdaptedVertexEdgeCount();
                    else if (_representationTypeValue == BuilderType.RawFrequencyRepresentationSlaks ||
                             _representationTypeValue == BuilderType.NormalizedFrequencyRepresentationSlaks)
                        distanceMeasureDelegate = new MCSAdaptedVertexEdgeFrequency();
                    else
                        throw new Exception(_representationTypeValue +
                                            " is not compatible with MCSadapted distance measure");

                    Console.WriteLine("MCSadapted distance algorithm set");
                    //writer.WriteLine("MCSadapted distance algorithm set");
                }
                else if (distAlgorithmName == distanceAlgorithmValue13)
                {
                    MCSedge_weight.EdgeWeight = edgeWeightValue;
                    Console.WriteLine("Edge weight paramete was set");
                    if (_representationTypeValue == BuilderType.StandardRepresentationSchenker ||
                        _representationTypeValue == BuilderType.ExtractorBuilderNodesOnly ||
                        _representationTypeValue == BuilderType.StandardRepresentationSlaks)
                        distanceMeasureDelegate =
                            new MCSedge_weight ();
                    else
                        throw new Exception(_representationTypeValue +
                                            " is not compatible with MCSedge-weight distance measure");

                    Console.WriteLine("MCSedge-weight distance algorithm set");
                    //writer.WriteLine("MCSedge-weight distance algorithm set");
                }
                else if (distAlgorithmName == distanceAlgorithmValue14)
                {
                    Console.WriteLine("WGUbalanced distance algorithm set");
                    if (_representationTypeValue == BuilderType.StandardRepresentationSchenker ||
                        _representationTypeValue == BuilderType.ExtractorBuilderNodesOnly ||
                        _representationTypeValue == BuilderType.StandardRepresentationSlaks)
                        distanceMeasureDelegate = new WGUbalanced ();
                    else
                        throw new Exception(_representationTypeValue +
                                            " is not compatible with WGUbalanced distance measure");

                    Console.WriteLine("WGUbalanced distance algorithm set");
                    //writer.WriteLine("WGUbalanced distance algorithm set");
                }
                else if (distAlgorithmName == distanceAlgorithmValue15)
                {
                    Console.WriteLine("WGUseparate distance algorithm set");
                    if (_representationTypeValue == BuilderType.StandardRepresentationSchenker ||
                        _representationTypeValue == BuilderType.ExtractorBuilderNodesOnly ||
                        _representationTypeValue == BuilderType.StandardRepresentationSlaks)
                        distanceMeasureDelegate = new WGUseparate ();
                    else
                        throw new Exception(_representationTypeValue +
                                            " is not compatible with WGUseparate distance measure");

                    Console.WriteLine("WGUseparate distance algorithm set");
                    //writer.WriteLine("WGUseparate distance algorithm set");
                }
                else if (distAlgorithmName == distanceAlgorithmValue16)
                {
                    Console.WriteLine("WGUsubgraph distance algorithm set");
                    if (_representationTypeValue == BuilderType.StandardRepresentationSchenker ||
                        _representationTypeValue == BuilderType.ExtractorBuilderNodesOnly ||
                        _representationTypeValue == BuilderType.StandardRepresentationSlaks)
                        distanceMeasureDelegate = new WGUSubgraphVertexEdgeCount ();
                    else
                        throw new Exception(_representationTypeValue +
                                            " is not compatible with WGUsubgraph distance measure");

                    Console.WriteLine("WGUsubgraph distance algorithm set");
                    //writer.WriteLine("WGUsubgraph distance algorithm set");
                }
                else if (distAlgorithmName == distanceAlgorithmValue7)
                {
                    if (_representationTypeValue == BuilderType.StandardRepresentationSchenker ||
                        _representationTypeValue == BuilderType.ExtractorBuilderNodesOnly ||
                        _representationTypeValue == BuilderType.StandardRepresentationSlaks ||
                        _representationTypeValue == BuilderType.SimpleRepresentationSchenker ||
                        _representationTypeValue == BuilderType.SimpleRepresentationSlaks ||
                        _representationTypeValue == BuilderType.NDistanceRepresentationSchenker ||
                        _representationTypeValue == BuilderType.NDistanceRepresentationSlaks ||
                        _representationTypeValue == BuilderType.NSimpleDistanceRepresentationSchenker ||
                        _representationTypeValue == BuilderType.NSimpleDistanceRepresentationSlaks)
                        distanceMeasureDelegate = new DistWGUVertexEdgeCountExtended();
                    else

                        throw new Exception(_representationTypeValue + " is not compatible with WGUext distance measure");

                    Console.WriteLine("WGUext distance algorithm set");
                    //writer.WriteLine("WGUext distance algorithm set");
                }
                else
                {
                    //writer.WriteLine("Uknown distance algorithm : " + distAlgorithmName +
                    //                 " -> Must be [MSC,WGU,SlaksMCS,SkaksWGU]");
                    distanceMeasureDelegate = null;
                    throw new Exception("Uknown distance algorithm : " + distAlgorithmName +
                                        " -> Must be [MSC,WGU,SlaksMCS,SkaksWGU]");
                }

            }
            else
            {
                //writer.WriteLine("No distance algorithm set");
                distanceMeasureDelegate = null;
                throw new Exception("No distance algorithm set");
            }

        }
    }
}
